const titleCase = str => {
  const words = str.split(' ');
  const titleWords = [];
  for (const word of words) {
    titleWords.push(word[0].toUpperCase() + word.slice(1).toLowerCase());
  }
  return titleWords.join(' ');
}

console.log(titleCase("I like to code"));